<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <div class="pl-6">
      <el-radio-group v-model="dateRange" @change="(value: any) => handleQueryList(value)">
        <el-radio-button
          v-for="item in ['today', 'yesterday', 'last7day', 'last30day', 'lastYear', 'all']"
          :key="item"
          :label="item"
        >
          {{ $t(`cms.access.${item}`) }}
        </el-radio-button>
      </el-radio-group>
    </div>
    <div class="mt-3 app-block">
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="时间" prop="dateString">
          <el-date-picker
            v-model="queryParams.dateString"
            value-format="YYYY-MM-DD"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <Table
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
    />
  </ContentWrap>
</template>
<script setup lang="ts" name="AccessPage">
import { allSchemas } from './accessPage.data'
import * as AccessPageApi from '@/api/cms/flowstatistic'
import { formatDate, addTimeByDay } from '@/utils/formatTime'
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
dayjs.extend(duration)
const queryFormRef = ref() // 搜索的表单
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
const { tableObject, tableMethods } = useTable({
  getListApi: AccessPageApi.getAccessPagePage // 分页接口
})
// 获得表格的各种操作
const { setSearchParams } = tableMethods
// 列表相关查询的变量
const queryParams = reactive({
  dateString: [],
  pageNo: 1,
  pageSize: 20
})
const initTime = () => {
  const now = formatDate(new Date(), 'YYYY-MM-DD')
  const begin = addTimeByDay(new Date(), -30, 'YYYY-MM-DD')
  queryParams.dateString = [begin, now]
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  setSearchParams(queryParams)
}
const handleQueryList = async (range: string) => {
  const now = formatDate(getEndByDateRange(range), 'YYYY-MM-DD')
  const begin = formatDate(getBeginByDateRange(range), 'YYYY-MM-DD')
  queryParams.dateString = [begin, now]
  queryParams.pageNo = 1
  setSearchParams(queryParams)
}

const dateRange = ref<string>('last30day')
const getBeginByDateRange = (range: string): string | undefined => {
  switch (range) {
    case 'now':
      return dayjs().subtract(2, 'hour').format()
    case 'today':
      return dayjs().startOf('day').format()
    case 'yesterday':
      return dayjs().startOf('day').subtract(1, 'day').format()
    case 'last7day':
      return dayjs().startOf('day').subtract(6, 'day').format()
    case 'last30day':
      return dayjs().startOf('day').subtract(29, 'day').format()
    case 'lastYear':
      return dayjs().startOf('day').subtract(1, 'year').format()
    default:
      return undefined
  }
}

const getEndByDateRange = (range: string): string => {
  if (range === 'now') {
    return dayjs().format()
  }
  if (range === 'yesterday') {
    return dayjs().endOf('day').subtract(1, 'day').format()
  }
  return dayjs().endOf('day').format()
}

const initQuery = () => {
  queryParams.pageNo = 1
  initTime()
  setSearchParams(queryParams)
}

/** 初始化 **/
onMounted(() => {
  initQuery()
})
</script>
